package com.zhuss.opus.opus;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.opus.opus.DetailOpusBo;
import com.zhuss.bean.bo.opus.opus.SearchOpusBo;
import com.zhuss.bean.bo.opus.opus.wechat.ListOpusBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.opus.opus.DetailOpusVo;
import com.zhuss.bean.vo.opus.opus.ListOpusVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.opus.processor.OpusAppendRead;
import com.zhuss.opus.processor.OpusRead;
import com.zhuss.opus.processor.OpusTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * opus查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("opusReadService")
public class OpusReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("opusRead")
	private OpusRead opusRead;

	@Autowired
	@Qualifier("opusTopicRead")
	private OpusTopicRead opusTopicRead;
	
	@Autowired
	@Qualifier("opusAppendRead")
	private OpusAppendRead opusAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailOpusBo bo = (DetailOpusBo) param;
		Long opusId = bo.getOpusId();
		DetailOpusVo vo = (DetailOpusVo) opusRead.detail(opusId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchOpusBo bo = (SearchOpusBo) param;
		Object result = opusRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailOpusBo param) throws WpsException {
		Long opusId = param.getOpusId();
		Object result = opusTopicRead.listTopicTree(opusId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listOpus(Object param) throws WpsException {
		ListOpusBo bo = (ListOpusBo) param;
		Object result = opusRead.listOpus(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailOpusVo> pageVo = (PageVo<DetailOpusVo>) result;
		for (DetailOpusVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = opusAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOpusId(), null).size();
			Integer collectNum = opusAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOpusId(), null).size();
			Integer forwardNum = opusAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOpusId(), null).size();
			Integer recommendNum = opusAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getOpusId(), null).size();
			Integer topicNum = opusTopicRead.listTopic(vo.getOpusId(), 0l).size();
			
			Integer thumbSelfNum = opusAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOpusId(), userId).size();
			Integer collectSelfNum = opusAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOpusId(), userId).size();
			Integer forwardSelfNum = opusAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOpusId(), userId).size();
			Integer recommendSelfNum = opusAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getOpusId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setRecommendNum(recommendNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			vo.setRecommendSelfNum(recommendSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListOpusVo> pageVo = (PageVo<ListOpusVo>) result;
		for (ListOpusVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = opusAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOpusId(), null).size();
			Integer collectNum = opusAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOpusId(), null).size();
			Integer forwardNum = opusAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOpusId(), null).size();
			Integer recommendNum = opusAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getOpusId(), null).size();
			Integer topicNum = opusTopicRead.listTopic(vo.getOpusId(), 0l).size();
			
			Integer thumbSelfNum = opusAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getOpusId(), userId).size();
			Integer collectSelfNum = opusAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getOpusId(), userId).size();
			Integer forwardSelfNum = opusAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getOpusId(), userId).size();
			Integer recommendSelfNum = opusAppendRead.listByType(AppendTypeEnum.推荐.key(), vo.getOpusId(), userId).size();
			
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setRecommendNum(recommendNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			vo.setRecommendSelfNum(recommendSelfNum);
			
		}
	}
	
	
}

